<template>
  <div class="building-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <div class="search-label">企业名称：</div>
      <!-- @clear="doSearch" 监听输入框清空事件，触发搜索 -->
      <el-input v-model="params.name" clearable placeholder="请输入内容" class="search-main" @clear="doSearch" />
      <el-button type="primary" @click="doSearch">查询</el-button>
    </div>
    <div class="create-container">
      <el-button type="primary" @click="$router.push('/addEnterprise')">添加企业</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table style="width: 100%" :data="enterpriseList" @expand-change="handleExpand">
        <!-- 展开行 通过设置 type="expand" 和 Scoped slot 可以开启展开行功能 -->
        <el-table-column type="expand">
          <template #default="{ row }">
            <el-table :data="row.rentList">
              <el-table-column label="租赁楼宇" width="320" prop="buildingName" />
              <el-table-column label="租赁起始时间" prop="startTime" />
              <el-table-column label="合同状态" prop="status">
                <template #default="scope">
                  <el-tag :type="formatInfoType(scope.row.status)">{{ formatStatus(scope.row.status) }}</el-tag>
                </template>
              </el-table-column>
              <el-table-column label="操作" width="180">
                <template #default="scope">
                  <!-- <el-button size="mini" type="text">续租</el-button> -->
                  <el-button size="mini" type="text" :disabled="scope.row.status === 3" @click="outRent(scope.row.id)">退租</el-button>
                  <el-button size="mini" type="text">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </template>
        </el-table-column>

        <el-table-column type="index" label="序号" />
        <el-table-column label="企业名称" width="320" prop="name" />
        <el-table-column label="联系人" prop="contact" />
        <el-table-column label="联系电话" prop="contactNumber" />
        <el-table-column label="操作">
          <template #default="{ row }">
            <el-button size="mini" type="text" @click="addRent(row.id)">添加合同</el-button>
            <el-button size="mini" type="text" @click="$router.push(`/exterpriseDetail?id=${row.id}`)">查看</el-button>
            <el-button size="mini" type="text" @click="$router.push(`/addEnterprise?id=${row.id}`)">编辑</el-button>
            <el-button size="mini" type="text" @click="deleteEnterprise(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page-container">
      <el-pagination
        layout="total, prev, pager, next"
        :total="total"
        :current-page="params.page"
        :page-size.sync="params.pageSize"
        @current-change="changePage"
      />
    </div>
    <!-- 添加合同 -->
    <RentDialog ref="rentDialog" :show-dialog.sync="showDialog" :enterprise-id="enterpriseId" @updateSuccess="getList" />
  </div>
</template>

<script>
import { deleteEnterpriseAPI, getEnterpriseListAPI, getRentListAPI, outRentAPI } from '@/api/enterprise'
import RentDialog from './components/rentDialog.vue'

export default {
  name: 'Enterprise',
  components: {
    RentDialog
  },
  data() {
    return {
      enterpriseList: [],
      total: 0,
      params: {
        page: 1,
        pageSize: 4,
        name: ''
      },
      showDialog: false,
      enterpriseId: ''
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 获取企业列表
    async getList() {
      const res = await getEnterpriseListAPI(this.params)
      this.total = res.data.total
      // 增加合同数据默认列表
      this.enterpriseList = res.data.rows.map(item => {
        return {
          ...item,
          rentList: [] // 合同列表
        }
      })
    },
    // 切换页码
    changePage(newPage) {
      this.params.page = newPage
      this.getList()
    },
    // 搜索企业
    doSearch() {
      this.params.page = 1
      this.getList()
    },
    // 删除企业
    deleteEnterprise(id) {
      this.$confirm('此操作将永久删除该条数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await deleteEnterpriseAPI(id)
        this.getList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      })
    },
    // 添加合同
    addRent(id) {
      this.showDialog = true
      this.enterpriseId = id
    },
    // 处理展开行数据
    async handleExpand(row, rows) {
      // console.log('展开行', row, rows)
      const isExpend = rows.find(item => item.id === row.id)
      if (isExpend) {
        const res = await getRentListAPI(row.id)
        // eslint-disable-next-line require-atomic-updates
        row.rentList = res.data
        console.log(res.data)
      }
    },
    // 格式化status
    formatStatus(status) {
      const MAP = {
        0: '待生效',
        1: '生效中',
        2: '已到期',
        3: '已退租'
      }
      return MAP[status]
    },
    // 格式化tag类型
    formatInfoType(status) {
      const MAP = {
        0: '',
        1: 'success',
        2: 'danger',
        3: 'info'
      }
      // return 格式化之后的中文显示
      return MAP[status]
    },
    // 合同退租
    outRent(rentId) {
      this.$confirm('确认退租吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 1. 调用接口
        await outRentAPI(rentId)
        // 2. 重新拉取列表
        this.getList()
        this.$message({
          type: 'success',
          message: '退租成功!'
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.department-container {
  padding: 10px;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  ;
  padding-bottom: 20px;

  .search-label {
    width: 100px;
  }

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}
.create-container{
  margin: 10px 0px;
}
.page-container{
  padding:4px 0px;
  text-align: right;
}
.form-container{
  padding:0px 80px;
}
</style>
